package com.tiza.xiaoyoubao.ui.fragment;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.tianze.library.base.BaseFragment;
import com.tianze.library.utils.AppUtils;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.app.UIHelper;
import com.tiza.xiaoyoubao.entity.EditInfo;
import com.tiza.xiaoyoubao.entity.UserInfo;
import com.tiza.xiaoyoubao.entity.bean.BankInfo;
import com.tiza.xiaoyoubao.net.ApiHttpClient;
import com.tiza.xiaoyoubao.net.JsonCallback;
import com.tiza.xiaoyoubao.net.ResponseInfo;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Request;

/**
 * Created by Administrator on 2017/4/25.
 */

public class BankInfoBindingFragment extends BaseFragment {

    @BindView(R.id.tv_chooseBank)
    TextView tv_chooseBank;
    @BindView(R.id.tv_chooseCity)
    TextView tv_chooseCity;
    @BindView(R.id.et_userName)
    EditText et_userName;
    @BindView(R.id.et_bankCardNum)
    EditText et_bankCardNum;
    @BindView(R.id.et_bankName)
    EditText et_bankName;
    @BindView(R.id.btn_commit)
    Button btn_commit;

    private String bank, bankCity, userName, bankCardNum, bankName;
    UserInfo userInfo = Hawk.get(Constants.KEY_USER_INFO, (UserInfo) null);
    BankInfo bankInfo;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_bankinfo_binding;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        bankInfo = (BankInfo) args.getSerializable("info");
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        if (bankInfo != null) {
            tv_chooseBank.setText(bankInfo.getBankName());
            tv_chooseBank.setTextColor(getResources().getColor(R.color.title2));
            tv_chooseCity.setText(bankInfo.getCity());
            tv_chooseCity.setTextColor(getResources().getColor(R.color.title2));
            et_userName.setText(bankInfo.getHolder());
            et_bankCardNum.setText(bankInfo.getAccount());
            et_bankName.setText(bankInfo.getCityBank());
            btn_commit.setText("修改");
        }
        intWatch();
    }

    @OnClick({R.id.tv_chooseBank, R.id.tv_chooseCity, R.id.btn_commit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_chooseBank:
                UIHelper.showBankName(mActivity);
                break;
            case R.id.tv_chooseCity:
                UIHelper.showBankCityPr(mActivity);
                break;
            case R.id.btn_commit:
                String message = checkBankInfo();
                if (TextUtils.isEmpty(message)) {
                    setBankInfo();
                } else {
                    toast(message);
                }
                break;
            default:
                break;
        }
    }


    @Subscriber(tag = Constants.TAG_GETCITY_SUCCESS)
    private void onEdit(EditInfo info) {
        int type = info.getType();
        switch (type) {
            case EditInfo.TYPE_GET_CITY_NAME:
                bankCity = info.getValue();
                tv_chooseCity.setText(bankCity);
                tv_chooseCity.setTextColor(getResources().getColor(R.color.title2));
                break;
            case EditInfo.TYPE_GET_BANK_NAME:
                bank = info.getValue();
                tv_chooseBank.setText(bank);
                tv_chooseBank.setTextColor(getResources().getColor(R.color.title2));
                break;
        }
    }

    private void setBankInfo() {
        ApiHttpClient.setBuyerBankInfo(BankInfoBindingFragment.this, bankCardNum, userName, bank,
                bankCity, bankName,
                userInfo.getWebzzCmsBuyerInfo().getUserid(),
                new JsonCallback<String>(String.class, true) {

                    @Override
                    public void onBefore(Request request) {
                        super.onBefore(request);
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        toast("银行卡保存失败");
                    }

                    @Override
                    public void onResponse(ResponseInfo<String> response) {
                        int code = response.getCode();
                        String message = response.getMessage();
                        if (code == 1) {
                            showWarnDialog(message);
                            return;
                        }
                        toast("银行卡绑定成功!");
                        // 通知绑定银行卡成功
                        EventBus.getDefault().post(true,
                                Constants.TAG_SET_BANKINFO_SUCCESS
                        );
                        finish();
                    }

                    @Override
                    public void onAfter() {
                        super.onAfter();
                    }
                }
        );
    }

    private String checkBankInfo() {
        bank = tv_chooseBank.getText().toString().trim();
        if (bank.equals("请选择以银行")) return "请选择银行";
        bankCity = tv_chooseCity.getText().toString().trim();
        if (bankCity.equals("请选择开户行所在城市")) return "请选择开户行所在城市";
        bankCardNum = et_bankCardNum.getText().toString().trim();
        if (!AppUtils.isBankCardNumber(bankCardNum)) return "请输入正确的银行卡号";
        userName = et_userName.getText().toString().trim();
        if (TextUtils.isEmpty(userName)) return "请输入持卡人姓名";
        bankName = et_bankName.getText().toString().trim();
        if (TextUtils.isEmpty(bankName)) return "请输入开户行名称";
        return "";
    }

    public void intWatch() {
        et_userName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                et_userName.setTextColor(getResources().getColor(R.color.title2));
            }
        });

        et_bankCardNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                et_bankCardNum.setTextColor(getResources().getColor(R.color.title2));
            }
        });
        et_bankName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
            @Override
            public void afterTextChanged(Editable s) {
                et_bankName.setTextColor(getResources().getColor(R.color.title2));
            }
        });
    }
}
